﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Things.Common;
using LTP.Accounts.Bus;
namespace Things.Web.CropsProduct
{
    public partial class Add : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {                
                BindCrops();
                BindFactory();

                if (Request.Params["CropsID"] != null && Request.Params["CropsID"].Trim() != "")
                {
                    Guid cropsID = Guid.Empty;
                    Guid.TryParse(Request.Params["CropsID"], out cropsID);
                    if (cropsID != Guid.Empty)
                    {
                        Things.Common.UICommon.SetSelectedDDL(ddlCrops, cropsID.ToString());
                        ddlCrops.Enabled = false;
                        btnGoCrops.Visible = true;
                    }
                }
                else
                {
                    ddlCrops.Enabled = true;
                    btnGoCrops.Visible = false;
                }

            }
        }

        protected void BindCrops()
        {
            Things.BLL.Crops farmerBLL = new BLL.Crops();
            DataSet ds = new DataSet();
            StringBuilder strWhere = new StringBuilder();
            strWhere.Append(" IsDeleted=0 ");
            ds = farmerBLL.GetList(strWhere.ToString());
            Things.Common.UICommon.BindDDL(ddlCrops, ds, "CropsName", "CropsID");
        }

        protected void BindFactory()
        {
            Things.BLL.Factory cropsCategoryBLL = new BLL.Factory();
            DataSet ds = new DataSet();
            ds = cropsCategoryBLL.GetAllList();
            Things.Common.UICommon.BindDDL(ddlFactory, ds, "FactoryName", "FactoryID");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            string strErr = "";

            Guid cropsID = Guid.Empty;
            if (Guid.TryParse(this.ddlCrops.SelectedValue, out cropsID) == false)
            {
                strErr += "请选择农产品名称！\\n";
            }

            Guid factoryID = Guid.Empty;
            if (Guid.TryParse(this.ddlFactory.SelectedValue, out factoryID) == false)
            {
                strErr += "请选择加工厂！\\n";
            }
            if (!PageValidate.IsDateTime(txtProductDate.Text))
            {
                strErr += "加工时间格式错误！\\n";
            }
            if(string.IsNullOrEmpty(this.txtProductionBatch.Text))
            {
                strErr += "请输入生产批次！\\n";
            }
            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            DateTime ProductDate = DateTime.Parse(this.txtProductDate.Text);            
            bool IsDeleted = false;
			string ProductionBatch=this.txtProductionBatch.Text;
			bool IsSaledOut=this.chkIsSaledOut.Checked;

            Things.Model.CropsProduct model = new Things.Model.CropsProduct();
            model.CropsID = cropsID;
            model.FactoryID = factoryID;
            model.ProductDate = ProductDate;
            model.IsDeleted = IsDeleted;
			model.ProductionBatch=ProductionBatch;
			model.IsSaledOut=IsSaledOut;

            Things.BLL.CropsProduct bll = new Things.BLL.CropsProduct();
            if (bll.Add(model) == true)
            {
                Things.BLL.Crops cropsBLL = new BLL.Crops();
                Things.Model.Crops crops = cropsBLL.GetModel(model.CropsID);
                if (crops != null)
                {
                    crops.IsProducted = true;
                    cropsBLL.Update(crops);
                }

                Things.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "add.aspx");
            }

        }


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("list.aspx");
        }

        protected void btnGoCrops_Click(object sender, EventArgs e)
        {
            Response.Redirect("../Crops/list.aspx");
        }
    }
}
